<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm('form')"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="请假类型" prop="leaveType">
            <el-radio-group v-model="form.leaveType" disabled>
              <el-radio
                v-for="item in leaveType"
                :key="item.value"
                :label="item.value"
                >{{ item.msg }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="起止日期" prop="startDate">
            <el-date-picker
              v-model="startEndTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
              disabled
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="请假事由" prop="reason">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入请假事由"
              v-model="form.reason"
              disabled
            >
            </el-input>
          </el-form-item>
          <el-divider>
            <span>审批建议</span>
          </el-divider>
          <el-form-item label="审批建议" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="2">同意</el-radio>
              <el-radio label="3">不同意</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { eduLeaveRecordUpdate } from '@/api/admin/teach/eduLeaveRecord.js'
import { tbDictDefinitionQueryEnumList } from '@/api/admin/system/dictionary'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '请假审批',
      dialogVisible: false,
      form: {
        endDate: undefined,
        leaveType: '1', //字典配置EDU_LEAVE_RECORD_TYPE;1-事假，2-病假，3…
        makeUpDate: undefined, //补课日期
        makeUpMissedLessons: undefined, //补课课时数量
        missedLessons: undefined, //请假课时数量
        reason: undefined, //请假事由
        startDate: undefined, //创建时间
        status: 1, //0-草稿，1-待审核，2-审核通过，3-不通过
        teacherId: 'admin', //教员id
        teacherName: '超级管理员', //教员姓名
      },
      rules: {
        status: [
          { required: true, message: '请选择审批建议', trigger: 'blur' },
        ],
      },
      leaveType: [],
      startEndTime: [], //起止日期
    }
  },
  mounted() {
    Bus.$on('showDialogLeaveAudit', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogLeaveAudit')
  },
  methods: {
    //显示弹窗
    init(row) {
      Object.assign(this.form, row)
      this.startEndTime = [row.startDate, row.endDate]
      this.queryType()
    },
    //查询课程类型
    queryType() {
      tbDictDefinitionQueryEnumList({ code: 'EDU_LEAVE_RECORD_TYPE' }).then(
        (res) => {
          this.leaveType = res.data.data
          this.dialogVisible = true
        },
      )
    },

    submitForm() {
      let { status } = this.form
      if (status == 0 || status == 1) {
        this.$message.warning('请选择审批建议')
        return
      }
      this.edit()
    },
    //新增
    edit() {
      eduLeaveRecordUpdate(this.form).then((res) => {
        this.done()
      })
    },
    done() {
      this.dialogVisible = false
      Bus.$emit('refreshLeaveList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
    },
  },
}
</script>